//
//  Status.swift
//  swft_weibo
//
//  Created by mac on 15/9/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage
class Status: NSObject {
    
    /// 微博创建时间
    var created_at: String?
    
    /// 微博ID
    var id: Int = 0
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source: String?
    
    /// 配图数组
    var pic_urls: [[String: AnyObject]]?
    
    /// 用户信息
//    var user : [String:AnyObject]?
    var user : User?
    
    var cellHight : CGFloat=0
    
    
    init(dic:NSDictionary) {
        
        super.init()
        
        dictionary2Model(dic)
    }
   /// 加载网络数据
   class func loadUserInformation(complete:([Status])->()){
    
        let path = "2/statuses/home_timeline.json"
    
        let params = ["access_token":UserAccount.aquireUserAccount()!.access_token!]
        
        NetworkTools.shareNetworkTools().GET(path, parameters: params, success: { (_, object) -> Void in
            
            //处理从服务返回来的数据
            let array = self.handleStatusData(object as! [String : AnyObject])
            //缓存从服务器发过来的配图数据，为的是在显示配图的时候，能显示出真实现尺寸
            saveCachesImages(array, complete: complete)
            //调用闭包，刷新tableview重新显示主面
//            complete(array)
            
            }) { (_, _) -> Void in
        }
    }
    ///同时缓存配图数据
    class func saveCachesImages(status:[Status],complete:([Status])->()){
    //创建一个gcd主
    let group = dispatch_group_create()
    
    for oneStatus in status{
    
        if oneStatus.pic_urls != nil{
            
            for oneURL in oneStatus.pic_urls!{
                //进入gcd组
                dispatch_group_enter(group)
                let str = oneURL["thumbnail_pic"] as! String
                let imageURL = NSURL(string: str)
                SDWebImageManager.sharedManager().downloadImageWithURL(imageURL, options: SDWebImageOptions(rawValue: 0), progress: nil, completed: { (_, _, _, _, _) -> Void in
                    
                    //退出gcd组
                    dispatch_group_leave(group)
                })
            
            }

        }
    
    }
    dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
        //调用闭包，刷新tableview重新显示主面
        complete(status)
    }
    }
    
   /// 处理从服务返回来的数据
   class func handleStatusData(object:[String:AnyObject])->([Status]){
        
        
        var array = [Status]()
        //拿到对应的字点转成模型
        //            print("\(object)")
        
        let statusArray = object["statuses"]
        
        for onestatus in statusArray as! [[String:AnyObject]] {
            
            let status=Status(dic: onestatus)
            
            array.append(status)
        }    
        return array
    
    }
    /**
    字点转模型
    
    :param: dic
    */
    func dictionary2Model(dic:NSDictionary){
    
        setValuesForKeysWithDictionary(dic as! [String : AnyObject])
    
    }
    /**
    去除当返回的值为空的情况
    
    :param: value
    :param: key
    */
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    /**
    拦截住字点中的user键，然后转成模型
    
    :param: value <#value description#>
    :param: key   <#key description#>
    */
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key=="user"{
        
            user=User(dic: value as! [String:AnyObject])
//            print(user?.profile_image_url)
            
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
}
